﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for GroupTinhnangController
/// </summary>
public class GroupTinhnangController:Interface
{
    private SqlConnection con;
	public GroupTinhnangController()
	{
	    con = ConnectDB.Connect();
        ConnectDB.Open();
	}

    public int Insert(object obj)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Insert_Group_Tinhnang";
            cmd.CommandType = CommandType.StoredProcedure;
            GroupTinhnang gtn = (GroupTinhnang)obj;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar, 50).Value = gtn.Name;
            cmd.Parameters.Add("@order", SqlDbType.Int).Value = gtn.Order;
            cmd.Parameters.Add("@status", SqlDbType.Bit).Value = gtn.Status;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public int Update(object obj)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Update_Group_Tinhnang";
            cmd.CommandType = CommandType.StoredProcedure;
            GroupTinhnang gtn = (GroupTinhnang)obj;
            cmd.Parameters.Add("@group_tinhnang_id", SqlDbType.Int).Value = gtn.GroupTinhnang_id;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar, 50).Value = gtn.Name;
            cmd.Parameters.Add("@order", SqlDbType.Int).Value = gtn.Order;
            cmd.Parameters.Add("@status", SqlDbType.Bit).Value = gtn.Status;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public int Delete(int id)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Delete_Group_Tinhnang";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@group_tinhnang_id", SqlDbType.Int).Value = id;
            
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public DataTable GetAll()
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_GroupTinhnang WHERE [status]=1 ORDER BY [order] ASC";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }
    public List<GroupTinhnang> getAllGroup()
    {
        List<GroupTinhnang> list = new List<GroupTinhnang>();
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_GroupTinhnang WHERE [status]=1 ORDER BY [order] ASC";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            foreach (DataRow item in dt.Rows)
            {
                GroupTinhnang gt = new GroupTinhnang(item);
                list.Add(gt);
            }
            return list;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }

    public DataTable GetByID(int id)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_GroupTinhnang WHERE group_tinhnang_id=@group_tinhnang_id AND [status]=1 ORDER BY [order] ASC";
            cmd.Parameters.AddWithValue("@group_tinhnang_id", id);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }
}